//新增班级
$('#addClassesBtn').click(function(){
    //获取用户选中专业的_id
    const subjectsId = $('#selectHeadTeacher').val();
    const name = $('#subjectsId').val();
    // console.log(subjectsId,name);
    $.ajax({
        url:'/classes/createClasses',
        type: 'POST',
        data: { subjectsId, name },
        success(res){
            console.log('新增班级的结果', res);
            showClasses();
        }
    })
})

 showClasses();

async function showClasses(){
    const classesData = await getClassesData();
    classesTableRender(classesData);
}

function getClassesData(){
    return new Promise((resolve,reject) => {
        $.ajax({
            url: '/classes/getClasses',
            type: 'GET',
            success(res) {
                console.log('班级数据', res);
                if(res.code){
                    resolve(res.result)
                }
            }
        })
    })
}
//渲染
function classesTableRender(data){
    const classesHtml = data.map(item => `
    <tr>
        <td>${item._id}</td>
        <td>${item.subjectsId.name}</td>
        <td>${item.name}</td>
        <td>
            <a href="#">修改</a>
            <a href="#">删除</a>
        <td>
    </tr>
    `).join('');
    $('#classesTb').html(classesHtml);
}

//渲染学生二级下拉
function getClassesBySubjectsId(subjectsId){
        $.ajax({
            url:'/classes/getClassesBySubjectsId',
            type:'GET',
            data:{
                subjectsId
            },
            success(res){
                if(res.code){
                    const classesHtml = res.result.map(item => `<option value="${item._id}">${item.name}</option>`).join('');
                    $('#ban1').html(classesHtml);
                }
            }
        })  
}
